package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.CompanyBaseInfo;
import com.cz.zslu.entity.StoteAirlines;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.ZslStore;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.service.IStoteAirlinesService;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.IZslStoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 客服 前端控制器
 * </p>
 *
 * @author 森森
 * @since 2018-12-15
 */
@Api(description = "店铺客服")
@RestController
@RequestMapping("/stoteAirlines")
public class StoteAirlinesController {

    @Autowired
    private IStoteAirlinesService iStoteAirlinesService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private IZslStoreService iZslStoreService;


    @ApiOperation("获取本店铺的客服")
    @IgnoreAuth
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    public ReturnObject<List<StoteAirlines>> getPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                     @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                     @ApiParam("storeId") @RequestParam Long storeId
                                                     ) {
        EntityWrapper<StoteAirlines> ew = new EntityWrapper<>();
        ew.eq("state", 0);
        if(storeId!=null){

            ew.eq("store_id", storeId);
        }
        ew.orderBy("crt_time", false);
        Page<StoteAirlines> obj = iStoteAirlinesService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("获取详情")
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<StoteAirlines> findInfoById(@ApiParam("id") @RequestParam Long id) {

        return new ReturnObject<>(0, "获取成功", iStoteAirlinesService.selectById(id));
    }


    @ApiOperation("新增客服/编辑客服")
    @RequestMapping(value = "/addInfo", method = RequestMethod.POST)
    public ReturnObject<StoteAirlines> addInfo(@ApiParam("客服名称") String name,
                                               @ApiParam("qq") String qq, @ApiParam("type") Integer type,
                                               @ApiParam("id") @RequestParam(defaultValue = "0") Long id) {
        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        if(id!=0){
            StoteAirlines info= iStoteAirlinesService.selectById(id);
            info.setAirlinesName(name);
            info.setAirlinesQq(qq);
            info.setUserId(BaseContextHandler.getUserID());
            info.setStoreId(sysUser.getStoreId());
            info.setType(type);
            iStoteAirlinesService.updateById(info);
            ZslStore zslStore = iZslStoreService.selectById(sysUser.getStoreId());
            zslStore.setQq(qq);
            iZslStoreService.updateById(zslStore);
            return new ReturnObject<>(0, "添加成功", info);
        }else{
            StoteAirlines info = new StoteAirlines();
            info.setAirlinesName(name);
            info.setAirlinesQq(qq);
            info.setUserId(BaseContextHandler.getUserID());
            info.setStoreId(sysUser.getStoreId());
            info.setType(type);
            iStoteAirlinesService.insert(info);
            ZslStore zslStore = iZslStoreService.selectById(sysUser.getStoreId());
            zslStore.setQq(qq);
            iZslStoreService.updateById(zslStore);
            return new ReturnObject<>(0, "添加成功", info);
        }

    }


    @ApiOperation("删除客服")
    @RequestMapping(value = "/delInfo", method = RequestMethod.POST)
    public ReturnObject delInfo(@ApiParam("id") Long id) {
        iStoteAirlinesService.deleteById(id);
        return new ReturnObject<>(0, "删除成功");
    }
}

